package Test;

import DB.SupplierDB;
import Model.Supplier;
import org.junit.Test;
import java.util.ArrayList;
import static org.junit.Assert.fail;

public class SupplierDBTest {

    @Test
    public void testInsertSupplier() throws Exception {
        SupplierDB supplierDB = new SupplierDB();
        int rc = supplierDB.insertSupplier(new Supplier(6, "Test supplier", "Sofiendalsvej 200", "Danmark", 99999999, "asd@test.dk"));

        System.out.println(rc);

        if (rc == 1) {
            System.out.println("Inserting supplier successful");
        }
        else {
            fail("Failed inserting supplier");
        }
    }

    @Test
    public void testFindSupplier() throws Exception {

        SupplierDB supplierDB = new SupplierDB();

        int find = 4;

        Supplier supplier = supplierDB.findSupplier(find);

        if (supplier != null) {
            System.out.println(
                    supplier.getId() + " " +
                    supplier.getName() + " " +
                    supplier.getAddress() + " " +
                    supplier.getCountry() + " " +
                    supplier.getPhoneNo() + " " +
                    supplier.getEmail());
        }
        else {
            fail("Error finding supplier - suppler is null");
        }
    }

    @Test
    public void testGetAllSuppliers() throws Exception {
        SupplierDB supplierDB = new SupplierDB();
        ArrayList<Supplier> supObj = supplierDB.getAllSuppliers(false);

        if (supObj != null) {
            for (Supplier s : supObj) {
                System.out.println(s.getName());
            }
        }
        else {
            fail("No suppliers found");
        }
    }

    @Test
    public void testUpdateSupplier() throws Exception {
        SupplierDB supplierDB = new SupplierDB();
        String updateValue = "Test123";

        Supplier supplier = new Supplier(10, updateValue, "Update test", "Danmark", 123, "asd@update.dk");

        supplierDB.updateSupplier(supplier);

        String supplierName = supplierDB.findSupplier(10).getName();

        if (supplierName.equals(updateValue)) {
            System.out.println("Update successful");
        }
        else {
            fail("Update failed");
        }
    }

    @Test
    public void testDeleteSupplier() throws Exception {
        SupplierDB supplierDB = new SupplierDB();
        Supplier supplier = new Supplier(1000, "Delete", "Delete", "Delete", 123, "Delete");
        supplierDB.insertSupplier(supplier);

        if (supplierDB.findSupplier(1000) != null) {

            supplierDB.deleteSupplier(1000);

            if (supplierDB.findSupplier(1000) == null) {
                System.out.println("Delete successful");
            }
            else {
                fail("Delete not successful");
            }
        }
        else {
            fail("Inserting for deletion failed");
        }
    }
}
